<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>28 下拉菜单封装(面向对象)</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            list-style: none;
        }

        #box > ul {
            display: flex;
            width: 300px;
            height: 40px;
            margin: 100px auto;
            justify-content: center;
            font-size: 20px;
            background-color: #369;
        }
        li{
            flex-grow: 1;
            display: flex;
            flex-direction: column;
            align-items: center;
            line-height:40px;
        }
        
        #box ul ul {
            width: 100%;
            background-color: #369369;
            display: none;
        }
    </style>
</head>
<body>
<div id="box">
    <ul>
        <li>一级菜单
            <ul>
                <li>二级菜单</li>
                <li>二级菜单</li>
                <li>二级菜单</li>
            </ul>
        </li>
        <li>一级菜单
            <ul>
                <li>二级菜单</li>
                <li>二级菜单</li>
                <li>二级菜单</li>
            </ul>
        </li>
        <li>一级菜单
            <ul>
                <li>二级菜单</li>
                <li>二级菜单</li>
                <li>二级菜单</li>
            </ul>
        </li>
    </ul>
</div>
</body>
</html>

<script>
    function List(id) {
        this.id = document.getElementById(id);
        this.lis = this.id.children[0].children;
    }

    List.prototype.init = function () {
        var that = this;
        var timer = null;
        for (var i = 0; i < that.lis.length; i++) {
            var obj = that.lis[i];
            obj.onmouseover = function () {
                var ul = this;
                clearTimeout(timer);
                timer = setTimeout(function () {
                    that.show(ul.children[0]);
                },300)
            }
            obj.onmouseout = function () {
                clearTimeout(timer);
                that.hide(this.children[0]);
            }
        }
    }

    List.prototype.show = function (obj) {
        obj.style.display = "block";
    }

    List.prototype.hide = function (obj) {
        obj.style.display = "none";
    }


    var list = new List("box");
    list.init();
</script>